package com.wzapp.sitposture.eshop.endpoint;

import com.wzapp.sitposture.eshop.pay.properties.AlipayProperties;
import com.wzapp.sitposture.eshop.pay.properties.WeChatPaymentProperties;
import com.icanpay.PaymentSetting;
import com.icanpay.enums.GatewayType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.math.BigDecimal;
import java.net.URI;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * 功能: APP客户端支付<br/>
 *
 * @author wangzhijun
 *
 **/
@RestController
@RequestMapping("/eShop/orderPays/app")
public class AppPayEndpoint {

	@Autowired
	private AlipayProperties alipayProperties;

	@Autowired
	private WeChatPaymentProperties weChatPaymentProperties;

	@GetMapping("/ali")
	public Map<String, String> Alipay() throws IOException, Exception {

		PaymentSetting paymentSetting = new PaymentSetting(GatewayType.Alipay);
//		paymentSetting.getMerchant().setAppId(alipayProperties.getAppid());
//		paymentSetting.getMerchant().setEmail(
//				alipayProperties.getSeller_email());
//		paymentSetting.getMerchant().setPartner(alipayProperties.getPartner());
//		paymentSetting.getMerchant().setKey(alipayProperties.getKey());
//		paymentSetting.getMerchant().setPrivateKeyPem(
//				alipayProperties.getPrivatekeypem());
//		paymentSetting.getMerchant().setPublicKeyPem(
//				alipayProperties.getPublicKeypem());
//		paymentSetting.getMerchant().setNotifyUrl(
//				new URI(alipayProperties.getNotifyurl()));
//		paymentSetting.getMerchant().setReturnUrl(
//				new URI(alipayProperties.getReturnurl()));
//
//		paymentSetting.getOrder().setOrderAmount(new BigDecimal("0.01"));
//		paymentSetting.getOrder().setOrderNo(
//				new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date()));
//		paymentSetting.getOrder().setSubject("alipay");

		return paymentSetting.buildPayParams();
	}

	@GetMapping("/wechat")
	public Map<String, String> WeChatPayment() throws IOException, Exception {

		PaymentSetting paymentSetting = new PaymentSetting(
				GatewayType.WeChatPayment);

		paymentSetting.getMerchant().setAppId(
				weChatPaymentProperties.getAppid());
		paymentSetting.getMerchant().setPartner(
				weChatPaymentProperties.getMch_id());
		paymentSetting.getMerchant().setKey(weChatPaymentProperties.getKey());
		paymentSetting.getMerchant().setNotifyUrl(
				new URI(weChatPaymentProperties.getNotifyurl()));
		paymentSetting.getMerchant().setReturnUrl(
				new URI(weChatPaymentProperties.getReturnurl()));

		paymentSetting.getOrder().setOrderAmount(new BigDecimal("0.01"));
		paymentSetting.getOrder().setOrderNo(
				new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date()));
		paymentSetting.getOrder().setSubject("wechatpayment");
		paymentSetting.getOrder().setSceneInfo(
				"{\"h5_info\": {\"type\":\"APP\",\"wap_url\": \"http://test.everygod.com:8000\",\"wap_name\": \"课程购买\"}}");

		return paymentSetting.buildPayParams();
	}

}
